package com.keyue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.CarParamInfoDO;
import com.keyue.domain.vo.CarParamInfoQueryVO;
import com.keyue.domain.vo.CarParamInfoVO;
import com.keyue.mapper.CarParamInfoMapper;
import com.keyue.service.CarParamInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 车辆参数信息(CarParamInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-07-02 11:44:57
 */
@Slf4j
@Service
public class CarParamInfoServiceImpl extends ServiceImpl<CarParamInfoMapper, CarParamInfoDO> implements CarParamInfoService {


    /**
     * 通过ID查询单条数据
     * <p>
     * carParamInfoQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(CarParamInfoQueryVO carParamInfoQueryVO) {
        if (Objects.isNull(carParamInfoQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        CarParamInfoDO CarParamInfoDo = getById(carParamInfoQueryVO.getId());
        if (Objects.isNull(CarParamInfoDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(CarParamInfoDo, CarParamInfoVO.class));
    }

    /**
     * 分页查询
     *
     * @param carParamInfoQueryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(CarParamInfoQueryVO carParamInfoQueryVO) {

        LambdaQueryWrapper<CarParamInfoDO> queryWapper = new LambdaQueryWrapper<CarParamInfoDO>()
                .eq(CarParamInfoDO::getEnableFlag, Constant.ENABLE_TEN)
                .orderByDesc(CarParamInfoDO::getId);
        Page page = new Page<>(carParamInfoQueryVO.getPage(), carParamInfoQueryVO.getPageSize());
        IPage<CarParamInfoDO> iPage = page(page, queryWapper);
        carParamInfoQueryVO.setTotal(Long.valueOf(iPage.getTotal()).intValue());
        return ServerResult.success();
    }

    /**
     * 新增数据
     *
     * @param carParamInfoVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(CarParamInfoVO carParamInfoVO) {
        //todo 校验
        if (Objects.nonNull(carParamInfoVO.getId())) {
            carParamInfoVO.setId(null);
        }
        CarParamInfoDO carParamInfoDo = BeanUtil.copyProperties(carParamInfoVO, CarParamInfoDO.class);

        save(carParamInfoDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param carParamInfoVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(CarParamInfoVO carParamInfoVO) {
        //todo 校验
        if (Objects.isNull(carParamInfoVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        CarParamInfoDO carParamInfoDo = BeanUtil.copyProperties(carParamInfoVO, CarParamInfoDO.class);

        updateById(carParamInfoDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param carParamInfoQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(CarParamInfoQueryVO carParamInfoQueryVO) {
        List<Long> ids = carParamInfoQueryVO.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<CarParamInfoDO> carParamInfoList = listByIds(ids);
        if (CollectionUtils.isEmpty(carParamInfoList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        LambdaUpdateWrapper<CarParamInfoDO> updateWrapper = new LambdaUpdateWrapper<CarParamInfoDO>()
                .in(CarParamInfoDO::getId, ids)
                .set(CarParamInfoDO::getEnableFlag, Constant.UNABLE_TWENTY);
        update(updateWrapper);
        return ServerResult.success();
    }
}
